import { getAllSurveyUnitList, getDistrictCascader } from '/@/api/demo/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { checkID } from '/@/utils/index';
import debounce from 'lodash.debounce';
import { TreeSelect } from 'ant-design-vue';
const { SHOW_PARENT, SHOW_ALL, SHOW_CHILD } = TreeSelect;
import { useUserStore } from '/@getAllSurveyUnitList/store/modules/user';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';

function getDistrictMany(value) {
  if (value) {
    const fds: string[] = [];
    value.forEach((element) => {
      fds.push(element.regionName);
    });

    if (fds.length > 0) {
      return fds.toString();
    }
  }

  return value;
}

export const columns: BasicColumn[] = [
  {
    title: '任务包名称',
    dataIndex: 'name',
  },
  {
    title: '任务包指派',
    dataIndex: 'surveyUnitName',
  },
  {
    title: '抽样比例',
    dataIndex: 'sampleProportion',
  },
  {
    title: '抽样条数',
    dataIndex: 'sampleCount',
  },
  {
    title: '初判结果',
    dataIndex: 'sampleType',
    customRender: ({ record }) => {
      const status = record.sampleType;
      let color = 'green';
      let text = '-/-';
      if (0 == status) {
        color = 'green';
        text = '无隐患';
      } else if (1 == status) {
        color = 'yellow';
        text = '存在一般隐患';
      } else if (2 == status) {
        color = 'red';
        text = '严重安全隐患';
      }
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '正确率',
    dataIndex: 'hc_acuracy',
    customRender: ({ record }) => {
      const hc_acuracy = record.hc_acuracy;
      return hc_acuracy + '%';
    },
  },
  {
    title: '街道',
    dataIndex: 'sampleAreaCodes',
    customRender: ({ record }) => {
      const sampleAreaCodes = record.sampleAreaCodes;
      return getDistrictMany(sampleAreaCodes);
    },
  },
  {
    title: '修改时间',
    dataIndex: 'modifiedOn',
    width: 200,
  },
];
// 核查任务管理(市级)表格 columns
export const columnsCity: BasicColumn[] = [
  {
    title: '任务包名称',
    dataIndex: 'name',
    customRender: ({ record }) => {
      const name = record.name;
      return name;
    },
  },
  {
    title: '任务包指派',
    dataIndex: 'surveyUnitName',
    customRender: ({ record }) => {
      const surveyUnitName = record.surveyUnitName;
      return surveyUnitName;
    },
  },
  {
    title: '抽样比例',
    dataIndex: 'sampleProportion',
  },
  {
    title: '抽样条数',
    dataIndex: 'sampleCount',
  },
  {
    title: '初判结果',
    dataIndex: 'sampleType',
    customRender: ({ record }) => {
      const status = record.sampleType;
      let color = 'green';
      let text = '-/-';
      if (0 == status) {
        color = 'green';
        text = '无隐患';
      } else if (1 == status) {
        color = 'yellow';
        text = '存在一般隐患';
      } else if (2 == status) {
        color = 'red';
        text = '严重安全隐患';
      }
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '正确率',
    dataIndex: 'hc_acuracy',
    customRender: ({ record }) => {
      const hc_acuracy = record.hc_acuracy;
      return hc_acuracy + '%';
    },
  },
  {
    title: '是否随机抽样',
    dataIndex: 'isRandom',
    customRender: ({ record }) => {
      const status = record.isRandom;
      let color = 'green';
      let text = '-/-';
      if (status) {
        color = 'green';
        text = '随机';
      } else {
        color = 'red';
        text = '手动';
      }
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '街道',
    dataIndex: 'sampleAreaCodes',
    customRender: ({ record }) => {
      const sampleAreaCodes = record.sampleAreaCodes;
      return getDistrictMany(sampleAreaCodes);
    },
  },
  {
    title: '修改时间',
    dataIndex: 'modifiedOn',
    width: 200,
    customRender: ({ record }) => {
      const modifiedOn = record.modifiedOn;
      const T = modifiedOn.substring(modifiedOn.indexOf('.'), modifiedOn.length); // 获取到小数点和后面的数字
      const T1 = modifiedOn.replace(T, ''); // 替换成 空（删除小数点和后面的数字）
      const T2 = T1.replace('T', ' ');
      return T2;
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '任务包名称',
    component: 'Input',
    labelWidth: '6em',
    colProps: { span: 8 },
  },
  {
    field: 'surveyUnitName',
    label: '任务包指派',
    component: 'Input',
    labelWidth: '6em',
    colProps: { span: 8 },
  },
];

export const accountManFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '任务包名称',
    component: 'Input',
    required: true,
  },
  {
    label: '任务包指派',
    field: 'surveyUnitId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllSurveyUnitList,
      labelField: 'name',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: '_fwbhArray',
    label: '房屋编号(fwbh)',
    helpMessage: '多个房屋编号之间换行符分割',
    component: 'InputTextArea',
    componentProps: {
      rows: 10,
      placeholder: '请输入一组房屋编号(fwbh),换行符分割',
    },
    required: true,
  },
];

export const addManFormSchema: FormSchema[] = [
  {
    field: '_fwbhArray',
    label: '房屋编号(fwbh)',
    helpMessage: '多个房屋编号之间换行符分割',
    component: 'InputTextArea',
    componentProps: {
      rows: 10,
      placeholder: '请输入一组房屋编号(fwbh),换行符分割',
    },
    required: true,
  },
];

export function getEditFormSchema(parId) {
  const accountFormSchema: FormSchema[] = [
    {
      field: 'name',
      label: '任务包名称',
      component: 'Input',
      required: true,
    },
    {
      label: '任务包指派',
      field: 'surveyUnitId',
      component: 'ApiSelect',
      componentProps: {
        api: getAllSurveyUnitList,
        labelField: 'name',
        valueField: 'id',
      },
      required: true,
    },
    {
      field: 'sampleProportion',
      label: '抽样比例',
      component: 'InputNumber',
      componentProps: {
        addonAfter: '%',
        min: 0,
        max: 100,
      },
      required: true,
    },
    {
      label: '初判结果',
      field: 'sampleType',
      component: 'Select',
      componentProps: {
        options: [
          { label: '无隐患', value: 0 },
          { label: '存在一般隐患', value: 1 },
          { label: '严重安全隐患', value: 2 },
        ],
        placeholder: '请选择初判结果',
      },
    },
    {
      field: 'sampleAreaCodes_',
      component: 'ApiCascader',
      helpMessage: ['双击也可以选择'],
      label: '街道',
      componentProps: ({ formModel, formActionType }) => {
        return {
          api: getDistrictCascader,
          apiParamKey: 'parentId',
          labelField: 'regionName',
          valueField: '_d',
          dataField: 'regionCode',
          asyncFetchParamKey: 'parentId',
          multiple: true,
          initFetchParams: {
            parentId: parId,
          },
          isLeaf: (record) => {
            return record.regionalLevel > 6;
          },
          onChange: (keys, args, apiData, options) => {},
        };
      },
    },
  ];

  return accountFormSchema;
}

export function getEditFormSchemaCity(parId) {
  const accountFormSchema: FormSchema[] = [
    {
      field: 'name',
      label: '任务包名称',
      component: 'Input',
      required: true,
    },
    {
      label: '任务包指派',
      field: 'surveyUnitId',
      component: 'ApiSelect',
      componentProps: {
        api: getAllSurveyUnitList,
        labelField: 'name',
        valueField: 'id',
      },
      required: true,
    },
    {
      field: 'sampleProportion',
      label: '抽样比例',
      component: 'InputNumber',
      componentProps: {
        addonAfter: '%',
        min: 0,
        max: 100,
      },
      required: true,
    },
    {
      field: 'startCs',
      label: '起始楼层',
      component: 'InputNumber',
    },
    {
      field: 'endCs',
      label: '终止楼层',
      component: 'InputNumber',
    },
    {
      label: '初判结果',
      field: 'sampleType',
      component: 'Select',
      componentProps: {
        options: [
          { label: '无隐患', value: 0 },
          { label: '存在一般隐患', value: 1 },
          { label: '严重安全隐患', value: 2 },
        ],
        placeholder: '请选择初判结果',
      },
    },
    {
      field: 'sampleAreaCodes_',
      component: 'ApiCascader',
      helpMessage: ['双击也可以选择'],
      label: '街道',
      componentProps: ({ formModel, formActionType }) => {
        return {
          api: getDistrictCascader,
          apiParamKey: 'parentId',
          labelField: 'regionName',
          valueField: '_d',
          dataField: 'regionCode',
          asyncFetchParamKey: 'parentId',
          multiple: true,
          initFetchParams: {
            parentId: parId,
          },
          isLeaf: (record) => {
            return record.regionalLevel > 6;
          },
          onChange: (keys, args, apiData, options) => {},
        };
      },
    },
  ];

  return accountFormSchema;
}
